<template>
  <div class="app-container">
    <div class="toolbar-section">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button plain @click="handleAdd">新增</el-button>
          <el-button plain @click="translation">打印</el-button>
        </el-col>
        <right-toolbar @queryTable="Refresh"></right-toolbar>
      </el-row>
    </div>
    <div class="table-section">
      <vxe-table v-loading="tableData.loading" :data="tableData.list" border stripe>
        <vxe-column type="checkbox" width="50" align="center" />
        <vxe-column width="100" title="状态" field="state" align="center">
          <template #default="{ row }">
            <dict-tag :options="apply_state" :value="row.state" />
          </template>
        </vxe-column>

        <vxe-column title="编号" field="number">
          <template #default="{ row }">
            <el-button style="margin-right: 10px;" link type="primary" size="small" @click="handleDetail(row)">
              {{ row.number }}
            </el-button>
          </template>
        </vxe-column>
        <vxe-column title="数量" field="num"></vxe-column>
        <vxe-column title="申请人" field="createUserName"></vxe-column>
        <vxe-column title="领用人" field="targetUserName"></vxe-column>
        <vxe-column title="备注" field="remark"></vxe-column>

        <vxe-column title="操作" align="center" class-name="small-padding fixed-width">
          <template #default="{ row }">
            <el-button style="margin-right: 10px;" link type="primary" size="small" v-if="row.state === 0 || row.state === 3" @click="handleSub(row)">
              提交
            </el-button>

            <el-button style="margin-right: 10px;" link type="primary" size="small"
              v-if="row.state === 0 || row.state === 3" @click="handleUpdate(row)">
              修改
            </el-button>

            <el-button v-if="row.state === 0 || row.state === 3" size="small" link type="primary"
              @click="handleDelete(row)">
              删除
            </el-button>
          </template>
        </vxe-column>
      </vxe-table>
    </div>
    <div class="pagination-section">
      <div class="pagination-container">
        <pagination v-show="tableData.paging.total > 0" :total="tableData.paging.total"
          v-model:page="tableData.paging.pageNum" v-model:limit="tableData.paging.pageSize" @pagination="getList" />
      </div>
    </div>


    <el-dialog v-model="isPrintdDialogVisible" width="50%">
      <printd-dialog ref="printdDialog" @sub="getList"></printd-dialog>
    </el-dialog>

    <edit :apply-type="applyType" ref="edit" @sub="handleAddSuccess"></edit>
    <detail ref="detailDialog"></detail>
  </div>

</template>

<script setup>
import api from "./api.js";
import Edit from './edit.vue'
import Detail from "@/components/Asset/ApplyDetailDialog/index.vue";
import PrintdDialog from "./Printdistribute.vue"

const edit = ref();
const detailDialog = ref();

const { proxy } = getCurrentInstance();
const { apply_state } = proxy.useDict("apply_state");

// 申请类型
const applyType = ref('ZCPF');

const queryParams = reactive({
  applyType: applyType.value,
});

const tableData = reactive({
  loading: false,
  list: [], // 表格列表数据
  paging: {
    total: 0, // 总条数
    pageNum: 1,
    pageSize: 10,
  }
});

const isPrintdDialogVisible = ref(false);

// 获取数据列表
function getList() {
  tableData.loading = true;
  const { pageNum, pageSize } = tableData.paging;

  const query = { ...queryParams };
  const pageReq = { page: pageNum - 1, size: pageSize };
  api.list(query, pageReq).then(response => {

    const { content, totalElements } = response;
    if (content && content.length > 0) {
      content.forEach(item => {
        if (item.formData) {
          let formData = JSON.parse(item.formData);
          Object.assign(item, formData);
        }
      });
    }

    tableData.list = content;
    tableData.paging.total = totalElements;
    tableData.loading = false;


  }).catch(() => {
    tableData.loading = false;
    proxy.$message.error("刷新失败");
  });
}

function Refresh() {
proxy.$message.success("刷新成功");

}
/** 新增按钮操作 */
function handleAdd() {
  // 触发新增对话框，新增成功后处理
  edit.value.open();
}

/** 新增操作成功后的回调 */
function handleAddSuccess() {
  proxy.$message.success("新增成功");
  getList();  // 刷新数据
}

/** 修改按钮操作 */
function handleUpdate(row) {
  edit.value.open(row);
}

function handleDetail(row) {
  detailDialog.value.open(row);
}

function translation() {
  isPrintdDialogVisible.value = true;
}

function handleSub(row) {
  api.sub(row.id).then(() => {
    getList();
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  this.$modal.confirm('是否确认删除').then(function () {
    return api.del(row.id);
  }).then(() => {
    getList();
    this.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

onMounted(() => {
  getList();
});
</script>

<style lang="scss" scoped>
</style>
